package com.accp.ssmoa.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.accp.ssmoa.dao.RoleMapper;
import com.accp.ssmoa.page.Page;
import com.accp.ssmoa.pojo.Role;
import com.accp.ssmoa.service.RoleService;

@Service("roleMapperServiceImpl")
public class RoleServiceImpl implements RoleService {
	
	@Autowired(required=false)
	private RoleMapper roleDAO;
	

	public int addRole(Role role) {
		
		return roleDAO.addRole(role);
	}


	public List<Role> findAll() {
		return roleDAO.findAll();
	}


	public int deleteRoles(List<Integer> ids) {
	
		return roleDAO.deleteRoles(ids);
	}


	public Role findById(Integer id) {
		return roleDAO.findById(id);
	}

	
	public int updateRole(Role role) {
		return roleDAO.updateRole(role);
	}


	public void showProductsByPage(HttpServletRequest request, Model model) {
		String pageNos = request.getParameter("pageNo");
		Page page = null;
		
		List<Role> roles = new ArrayList<Role>();
		
		Integer totalCount = roleDAO.getCount();
		if(pageNos != null) {
			
			page = new Page(totalCount, Integer.parseInt(pageNos));
			roleDAO.selectByPage(page.getStartPos(), page.getPageSize());
			
		} else {
			page = new Page(totalCount, 1);
			this.roleDAO.selectByPage(page.getStartPos(), page.getPageSize());
		}

		model.addAttribute("role", roles);
		model.addAttribute("page", page);
		}


	//你可以自己添加分页数据，不要就删了
	public int getTotalsize() {
		
		int totalsize=roleDAO.findAll().size();
		
		
		return totalsize;
	}


	public List<Role> getPageRole(Integer startRow, Integer pageSize) {
		
		
		return roleDAO.selectByPage(startRow, pageSize);
	}


	public List<Object> getRoleTree() {
		
		List<Role> list=roleDAO.findAll();
		List<Object> listTree = new ArrayList<Object>(); 
		//字符串拼接，形成json格式树形图数据
		String str = ""; 
		        for (int i = 0; i < list.size(); i++) {  
		        	Role  role = list.get(i);//遍历
		       str = "{id:'" +role.getId() + "', name:\""+role.getName()+"\" }";//封装ztree需要格式的字符串  
		       listTree.add(str);  
		   }

		return listTree;
	}


	public List<Role> getRolesByUserid(Integer userid) {
		
		return roleDAO.getRolesByUserid(userid);
	}


	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

